﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;
using System.Data;
using System.Collections;
using System.Data.Common;

namespace Cashier_Service
{
    public class BillPurchaseService
    {
        /// <summary>
        /// 查询进货单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="purchaseno"></param>
        /// <returns></returns>
        public BillPurchase GetBillPurchase(long id, String purchaseno)
        {
            BillPurchase billPurchase = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    billPurchase = new BillPurchase();
                    billPurchase.Id = id;
                    billPurchase.PurchaseNo = purchaseno;
                    billPurchase = new BillPurchaseDAO().GetBillPurchase(conn, billPurchase);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return billPurchase;
        }

        /// <summary>
        /// 查询进货单列表
        /// </summary>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">页面大小</param>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="purchaseno">进货单号</param>
        /// <param name="supplierno">供应商代码</param>
        /// <param name="adminUsername">员工号</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="debtAmount">欠款金额</param>
        /// <param name="ascfield">升序</param>
        /// <param name="descfield">降序</param>
        /// <returns></returns>
        public DataTable GetBillPurchaseTable(int pageindex, int pagesize, String enterpriseNo, String storeNo, String purchaseno, String supplierno,
            String adminUsername, String startDate, String endDate, Decimal debtAmount, String ascfield, String descfield)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillPurchase billPurchase = new BillPurchase();
                    billPurchase.Pageindex = pageindex;
                    billPurchase.Pagesize = pagesize;
                    billPurchase.EnterpriseNo = enterpriseNo;
                    billPurchase.StoreNo = storeNo;
                    billPurchase.PurchaseNo = purchaseno;
                    billPurchase.SupplierNo = supplierno;
                    billPurchase.AdminUsername = adminUsername;
                    billPurchase.StartDate = startDate;
                    billPurchase.EndDate = endDate;
                    billPurchase.DebtAmount = debtAmount;
                    billPurchase.Ascfield = ascfield;
                    billPurchase.Descfield = descfield;
                    dt = new BillPurchaseDAO().GetBillPurchaseTable(conn, billPurchase);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 查询进货单记录数
        /// </summary>
        /// <param name="enterpriseNo">企业业务号</param>
        /// <param name="storeNo">门店号</param>
        /// <param name="purchaseno">进货单号</param>
        /// <param name="supplierno">供应商代码</param>
        /// <param name="adminUsername">员工号</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="debtAmount">欠款金额</param>
        /// <returns></returns>
        public long GetBillPurchaseRecord(String enterpriseNo, String storeNo, String purchaseno, String supplierno, String adminUsername,
            String startDate, String endDate, Decimal debtAmount)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    BillPurchase billPurchase = new BillPurchase();
                    billPurchase.EnterpriseNo = enterpriseNo;
                    billPurchase.StoreNo = storeNo;
                    billPurchase.PurchaseNo = purchaseno;
                    billPurchase.SupplierNo = supplierno;
                    billPurchase.AdminUsername = adminUsername;
                    billPurchase.StartDate = startDate;
                    billPurchase.EndDate = endDate;
                    billPurchase.DebtAmount = debtAmount;
                    record = new BillPurchaseDAO().GetBillPurchaseRecord(conn, billPurchase);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 新增进货单
        /// </summary>
        /// <param name="billPurchase"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public long InsertBillPurchase(BillPurchase billPurchase, ArrayList list)
        {
            long id = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    conn.Open();
                    trans = conn.BeginTransaction();
                    id = new BillPurchaseDAO().InsertBillPurchase(conn, billPurchase);
                    if (id > 0 && list!=null)
                    {
                        BillPurchaseGoodsDAO purchaseGoodsDAO = new BillPurchaseGoodsDAO();
                        foreach (BillPurchaseGoods lopPurchaseGoods in list)
                        {
                            purchaseGoodsDAO.InsertBillPurchaseGoods(conn, lopPurchaseGoods);
                        }
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    if (trans != null)
                        trans = null;
                }
            }
            return id;
        }

        /// <summary>
        /// 更新进货单
        /// </summary>
        /// <param name="billPurchase"></param>
        /// <returns></returns>
        public int UpdateBillPurchase(BillPurchase billPurchase)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    flag = new BillPurchaseDAO().UpdateBillPurchase(conn, billPurchase);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除进货单
        /// </summary>
        /// <param name="purchasenos"></param>
        /// <returns></returns>
        public int DeleteBillPurchase(String purchasenos)
        {
            int count = 0;
            using (DbConnection conn = ConnectionFactory.GetDbConnection())
            {
                DbTransaction trans = null;
                try
                {
                    if (!String.IsNullOrEmpty(purchasenos))
                    {
                        conn.Open();
                        trans = conn.BeginTransaction();
                        String[] arr = purchasenos.Split(new Char[] { ',' });
                        BillPurchaseDAO purchaseDAO = new BillPurchaseDAO();
                        BillPurchase purchase = new BillPurchase();
                        BillPurchaseGoodsService purchaseGoodsService = new BillPurchaseGoodsService();
                        foreach (String lopPurchaseno in arr)
                        {
                            purchase.PurchaseNo = lopPurchaseno;
                            int flag = purchaseDAO.DeleteBillPurchase(conn, purchase);
                            if (flag > 0)
                            {
                                purchaseGoodsService.DeleteBillPurchaseGoods(conn, lopPurchaseno);
                                count++;
                            }
                        }
                        trans.Commit();
                    }
                }
                catch (Exception ex)
                {
                    if (trans != null)
                        trans.Rollback();
                    throw ex;
                }
                finally
                {
                    if (trans != null)
                        trans = null;
                }
            }
            return count;
        }

        /// <summary>
        /// 更新进货单欠款金额
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="purchaseno"></param>
        /// <param name="debtAmount"></param>
        /// <returns></returns>
        public int UpdateDebtAmount(DbConnection conn, String purchaseno, Decimal debtAmount)
        {
            int flag = 0;
            try
            {
                BillPurchase purchase = new BillPurchase();
                purchase.PurchaseNo = purchaseno;
                purchase.DebtAmount = debtAmount;
                flag = new BillPurchaseDAO().UpdateDebtAmount(conn, purchase);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
    }
}
